angular.module('starter.controllers')
.controller('ExhibitionCtrl', function($scope, museumFac, $stateParams) {
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //
  //$scope.$on('$ionicView.enter', function(e) {
  //});

  $scope.getOrderDetail = function() {
    museumFac.order.ticketorderdetail({
      orderId: $stateParams.exhibitionId
    },function(data){
      $scope.order = data.info;
    },function(data){
      alert(data.data.info);
    });
  };

  $scope.getExhibitionDetail = function() {
    museumFac.info.exhibitiondetail({
      exhibitionId: $stateParams.exhibitionId
    },function(data){
      $scope.exhibition = data.info;
    },function(data){
      alert(data.data.info);
    });
  };

  $scope.orderpay = function(num) {
    museumFac.wechat.confirmtorder({
      ticket_id: 1,
      amount: num
    }, function(response) {
      var res = response;
      if (res.info) {
        alert(res.info);
        return;
      }
      // console.log(res.redirect);
      var jsApiCall = function(data) {
        WeixinJSBridge.invoke(
          'getBrandWCPayRequest',
          data,
          function(res) {
            WeixinJSBridge.log(res.err_msg);
                  // alert(res.err_code+res.err_desc+res.err_msg);
                  if (res.err_msg == 'get_brand_wcpay_request:ok') {
                    alert("支付成功!");
                    $state.go('base.home');
                  } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                    alert("已取消支付!");
                  } else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                    alert("支付失败!");
                  } else {
                    alert('支付失败' + res.err_msg);
                  };
                }
                );
      }
      jsApiCall(res);
    }, function(data) {
      alert(data.data.info);
    });
  }
})